package edu.dali.enterprise_test.controller.menu;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import edu.dali.enterprise_test.dao.mapper.classification.SubMapper;
import edu.dali.enterprise_test.model.Pager;
import edu.dali.enterprise_test.model.vo.CategoryOps;
import edu.dali.enterprise_test.model.vo.Dto;
import edu.dali.enterprise_test.service.classification.MainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
@RequestMapping("/mainc")
public class MaincController {

    @Autowired
    private MainService mainService;

    @Autowired
    private SubMapper subMapper;

    @Autowired
    private HttpServletRequest request;

    @RequestMapping("index")
    public String index() {
        return "main/index";
    }

    @RequestMapping("getData")
    @ResponseBody
    public Pager<Dto> getData(int page , int limit) {
        PageHelper.startPage(page,limit);
        Dto params = new Dto();
        return mainService.selectList(params);
    }

    @RequestMapping("addDialog")
    public String addDialog() {
        return "main/add";
    }

    @RequestMapping("updateDialog/{id}")
    public String updateDialog(@PathVariable String id , Model model) {
        Dto dto = mainService.selectOne(id);

        model.addAttribute("info" , dto);
        return "main/update";
    }

    @PostMapping("insert")
    @ResponseBody
    public Map<String , Object> insert(String item , String description) {
        Map<String , Object> res = new HashMap<>();
        Dto dto = new Dto();
        dto.put("item" , item);
        dto.put("description" , description);
        mainService.insert(dto);
        res.put("code" , 0);
        res.put("msg" , "添加成功");
        return res;
    }
    @PostMapping("update")
    @ResponseBody
    public Map<String , Object> update(String id ,String item , String description) {
        Map<String , Object> res = new HashMap<>();
        Dto dto = new Dto();
        dto.put("id" , id);
        dto.put("item" , item);
        dto.put("description" , description);
        mainService.update(dto);

        res.put("code" , 0);
        res.put("msg" , "添加成功");
        return res;
    }

    @PostMapping("getOptions")
    @ResponseBody
    public Map<String, List<CategoryOps>> getOptions() {
        List<CategoryOps> list = subMapper.selectOptions();
        Map<String, List<CategoryOps>> items = list.stream().collect(Collectors.groupingBy(CategoryOps::getPname));
        return items;
    }

    @PostMapping("delete")
    @ResponseBody
    public Map<String , Object> delete(String id) {
        Map<String , Object> res = new HashMap<>();
        mainService.delete(id);
        res.put("code" , 0);
        res.put("msg" , "删除成功");
        return res;
    }
}
